package com.toannv.gameController;

import com.toannv.card.card;
import com.toannv.extension.RoomExtension;

public class CheckU{
	private card[] Cards;
	private int lenght;
	
	//tao doi tuong checkU
	public CheckU(card[] card){
		this.lenght = card.length;
		
		for(int i=0; i<card.length; i++){
			this.Cards[i] = card[i]; 
		}
	}
	
	//get length
	public int getLength(){
		return this.lenght;
	}
	
	//get index of card
	public card getCardOfIndex(int i){
		if(i<0 || i>lenght){
			return null;
		}
		
		return this.Cards[i];
	}
	
	//check U ngang sau do check u doc
	public static int checkNgangDoc(card[] Card){
		QuickSortCard Sort = new QuickSortCard();
		Sort.sortNumber(Card);//sap xep bo bai theo thu tu cac quan bai, neu bang diem nhau thi sap xep theo chat bai
		
		card ka[][] = new card[10][10];//khai bao mang chua cac ka ngang qua bo bai. toi da co 10 ka
		
		///////////////////Check phong ngang///////////////////////////////////////
		int colIndex = 0;
		int rowIndex = 0;
		//phan tu dau tien ka[0][0] duoc gan cho quan bai dau tien
		ka[colIndex][rowIndex] = new card(Card[0]);
		//Duyet qua toan bo mang cac quan bai
		for(int i=1; i<Card.length; i++){
			//neu diem cua ka tai vi tri colIndex va rowIndex bang diem cua quan bai thu i
			if(ka[colIndex][rowIndex].getNumber() == Card[i].getNumber()){
				//rowIndex tang len 1
				rowIndex++;
				//gan gia tri cua phan tu card thu i cho ka tai vi tri colIndex va rowIndex
				ka[colIndex][rowIndex] = new card(Card[i]);
			}
			//neu khong thi tang colIndex len 1 va rowIndex bang 0
			else{
				colIndex++;
				rowIndex = 0;
				//gan gi tri cua card tai vi tri i cho ka tai colIndex va rowIndex
				ka[colIndex][rowIndex] = new card(Card[i]);
			}
		}
		//so card con du la 0
		int cardDu = 0;
		
		//duyet qua toan bo mang hai chieu ka de dem so phom va so card con du
		for(int i=0; i<ka.length; i++){
			//neu gia tri number cua ka tai vi tri i 0 thi thoat khoi vong lap 
			if(ka[i][0] == null){
				break;
			}
			//vong lap duy mang doc cua mang ka tai vi tri thu i
			for(int j=0; j<ka[i].length; j++){
				//neu tai vi tri thu i j ma ka co gia tri number = 0
				if(ka[i][j] == null){
					//neu j>2 thi ka tren la 1 phom thoat khoi vong lap
					if(j>2){
						break;
					}
					//neu j<2 thi ka tai vi tri i j ko la phom va can phai dem vao phan tu cardDu 
					else if(j<=2){
						cardDu += (j);
						break;
					}
				}
			}
		}
		//neu card bang 0 thi tra ve gia tri 2 tuc la u tron 
		if(cardDu == 0){
			return 2;
		}
		//neu card du bang 1 thi tra ve gia tri 1 tuc la u 
		else if(cardDu == 1){
			return 1;
		}
		//neu card du bang 2 thi tra ve gia tri la 2 tuc la khong u
		else if(cardDu == 2){
			return 0;
		}
		
		//neu ma card du > 2
		//khai bao mot mang card doc de luu tru cac quan bai card du dung de check doc
		card[] CardDoc = new card[cardDu];
		//khai bao phan tu cua mang
		int element = 0;
		//chay qua toan bo mang ka de gan cac card du vao mang cardDoc
		for(int i=0; i<ka.length; i++){
			if(ka[i][0]==null){
				break;
			}
			
			for(int j= 0; j<ka[i].length; j++){
				if(ka[i][j] == null){
					if(j>2){
						break;
					}
					else if(j<=2){
						for(int t=0; t<j; t++){
							CardDoc[element] = new card(ka[i][t]);
							element++;
						}
					}
					break;
				}
			}
		}
		
		//sap xep mang card doc theo chieu tang dan cua cac chat. neu co so chat bang nhau se sap xep tang dan theo so diem moi quan bai
		Sort.sortPoker(CardDoc);
		//khai bao mang 2 chieu luu tru cac ka doc
		card kaDoc[][] = new card[cardDu][cardDu];
		colIndex = 0;
		rowIndex = 0;
		
		kaDoc[0][0] = new card(CardDoc[0]);//phan tu dau tien cua mang ka doc dc gan cho gia tri dau tien cua mang card doc
		//duyet qua toan bo mang card doc
		for(int i=1; i<CardDoc.length; i++){
			//neu diem so cua ka doc tai vi tri colIndex va rowIndex cong 1 bang diem so cua card doc tai vi tri i va so chat cua 2 quan bang
			//nhau thi so rowIndex se tang len 1 va quan bai tai vi tri thu i se duoc luu trong ka doc
			if((kaDoc[colIndex][rowIndex].getNumber()+1 == CardDoc[i].getNumber()) &&
					kaDoc[colIndex][rowIndex].getPoker() == CardDoc[i].getPoker()){
				rowIndex++;
				kaDoc[colIndex][rowIndex] = new card(CardDoc[i]);
			}
			
			//neu khong thi colindex tang len 1 va row index = 0
			else{
				colIndex++;
				rowIndex=0;
				//gan quan bai tai vi tri thu i cho ka doc
				kaDoc[colIndex][rowIndex] = new card(CardDoc[i]);
			}
		}
		//gan gia tri card du bang 0
		cardDu = 0;
		//duyet qua mang card doc de dem so card du
		for(int i=0; i<kaDoc.length; i++){
			if(kaDoc[i][0]==null){
				break;
			}
			for(int j=0; j<kaDoc[i].length; j++){
				if(kaDoc[i][j]==null){
					if(j>2){
						break;
					}
					else if(j<=2){
						cardDu += j;
						break;
					}
				}
			}
		}
		
		//neu so card du = 0 thi tra ve 2 tuc la u tron
		if(cardDu == 0){
			return 2;
		}
		//neu so card du = 1 thi tra ve 1 tuc la u
		else if(cardDu == 1){
			return 1;
		}
		//neu so card du >=2 thi tra ve 0 tuc la khong u
		else if(cardDu >= 2){
			return 0;
		}
		
		return 0;
	}
	
	// ham xay dung de check u doc sau do u ngang cho mot mang 10 quan bai
	public static int checkDocNgang(card[] Card){
		//sap xep bo bai theo thu tu cac chat. neu co so chat bang nhau se sap xep tang dan theo thu tu cac quan
		QuickSortCard Sort = new QuickSortCard();
		Sort.sortPoker(Card);
		//khai bao mot mang 2 chieu co kich thuoc 10 10 de luu tru cac ka doc
		card kaDoc[][] = new card[10][10];
		
		int colIndex = 0;
		int rowIndex = 0;
		kaDoc[colIndex][rowIndex] = new card(Card[0]);//gan gia tri dau cua mang cho ka[0][0]
		//gan gia tri cua quan bai cho mang 2 chieu kaDoc
		for(int i=1; i<Card.length; i++){
			//neu phu hop la ka thi se tang row index len 1 va gan gia tri
			if((kaDoc[colIndex][rowIndex].getNumber()+1 == Card[i].getNumber()) &&
					kaDoc[colIndex][rowIndex].getPoker() == Card[i].getPoker()){
				rowIndex++;
				kaDoc[colIndex][rowIndex] = new card(Card[i]);
			}
			//neu khong thi colindex tang len 1 va row index = 0
			else{
				colIndex++;
				rowIndex=0;
				kaDoc[colIndex][rowIndex] = new card(Card[i]);
			}
		}
		//khai bao so card du
		int CardDu = 0;
		//duyet qua toan bo mang kadoc de dem so card du
		for(int i=0; i<kaDoc.length; i++){
			if(kaDoc[i][0] == null){
				break;
			}
			for(int j=0; j<kaDoc[i].length; j++){
				if(kaDoc[i][j]==null){
					if(j>2){
						break;
					}
					else if(j<=2){
						CardDu += (j);
						break;
					}
				}
			}
		}
		//neu so card du bang 0 thi tra ve 2 tuc la u tron
		if(CardDu == 0){
			return 2;
		}
		//neu so card du bang 1 thi tra ve 1 tuc la u
		else if(CardDu == 1){
			return 1;
		}
		//neu so card du bang 2 thi tra ve 0 tuc la khong u
		else if(CardDu == 2){
			return 0;
		}

		//khai bao mang card ngang dung de luu tru ve check u ngang cho cac quan card du
		card[] CardNgang = new card[CardDu];
		int element = 0;//phan tu cua mang
		//duyet qua toan bo mang hai chieu ka doc de gan cac phan tu card du cho mang card ngang
		for(int i=0; i<kaDoc.length; i++){
			if(kaDoc[i][0]==null){
				break;
			}
			
			for(int j= 0; j<kaDoc[i].length; j++){
				if(kaDoc[i][j]==null){
					if(j>2){
						break;
					}
					else if(j<=2){
						for(int t=0; t<j; t++){
							CardNgang[element] = new card(kaDoc[i][t]);
							element++;
						}
					}
					break;
				}
			}
		}
		
		//sap xep lai mang card ngang theo thu tu tang dan so diem cac quan bai
		Sort.sortNumber(CardNgang);
		
		colIndex = 0;
		rowIndex = 0;
		card KaNgang[][] = new card[CardDu][CardDu];
		
		KaNgang[colIndex][rowIndex] = new card(CardNgang[0]);
		
		//duyet qua toan bo mang card ngang de kiem tra ka
		for(int i=1; i<CardNgang.length; i++){
			//neu gia tri number cua ka ngang bang gia tri number cua card ngang thu i thi row index tang len 1 vaf gia tri ka ngang dc gan
			if((KaNgang[colIndex][rowIndex].getNumber() == CardNgang[i].getNumber())){
				rowIndex++;
				KaNgang[colIndex][rowIndex] = new card(CardNgang[i]);
			}
			//neu khong thi colindex ++ va rowindex =0
			else{
				colIndex++;
				rowIndex=0;
				KaNgang[colIndex][rowIndex] = new card(CardNgang[i]);
			}
		}
		
		CardDu = 0;//card du = 0
		
		//duyet qua toan bo mang hai chieu ka ngang de check so card du
		for(int i=0; i<KaNgang.length; i++){
			if(KaNgang[i][0]==null){
				break;
			}
			for(int j=0; j<KaNgang[i].length; j++){
				if(KaNgang[i][j]==null){
					if(j>2){
						break;
					}
					else if(j<=2){
						CardDu += j;
					}
				}
			}
		}
		//neu so card du bang 0 thi tra ve 2 tuc la u tron
		if(CardDu == 0){
			return 2;
		}
		//neu so card du bang 1 thi tra ve 1 tuc la u
		else if(CardDu == 1){
			return 1;
		}
		//neu so card du >=2 thi tra ve 0 tuc la khong u
		else if(CardDu >= 2){
			return 0;
		}
		
		return 0;
	}
	
	public static int checkall(int idSender, card[] Cards){
		int checkDocNgang, checkNgangDoc;
		checkDocNgang = CheckU.checkDocNgang(Cards);
		checkNgangDoc = CheckU.checkNgangDoc(Cards);
		
		if(idSender==0){
			if(RoomExtension.CardOfUser0Eat.size()==3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 5;//u den tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 3;//u den
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 5;//u den tron
				}
				else if(checkDocNgang==0 && checkNgangDoc==0){
					return 1;//an lao
				}
			}
			else if(RoomExtension.CardOfUser0Eat.size()<3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 4;//u tron
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 4;//u tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 2;//u thuong
				}
				else return 0;//khong u
			}
		}
		else if(idSender==1){
			if(RoomExtension.CardOfUser1Eat.size()==3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 5;//u den tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 3;//u den
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 5;//u den tron
				}
				else if(checkDocNgang==0 && checkNgangDoc==0){
					return 1;//an lao
				}
			}
			else if(RoomExtension.CardOfUser1Eat.size()<3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 4;//u tron
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 4;//u tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 2;//u thuong
				}
				else return 0;//khong u
			}
		}
		else if(idSender==2){
			if(RoomExtension.CardOfUser2Eat.size()==3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 5;//u den tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 3;//u den
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 5;//u den tron
				}
				else if(checkDocNgang==0 && checkNgangDoc==0){
					return 1;//an lao
				}
			}
			else if(RoomExtension.CardOfUser2Eat.size()<3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 4;//u tron
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 4;//u tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 2;//u thuong
				}
				else return 0;//khong u
			}
		}
		else if(idSender==3){
			if(RoomExtension.CardOfUser3Eat.size()==3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 5;//u den tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 3;//u den
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 5;//u den tron
				}
				else if(checkDocNgang==0 && checkNgangDoc==0){
					return 1;//an lao
				}
			}
			else if(RoomExtension.CardOfUser3Eat.size()<3){
				if((checkDocNgang==2&&checkNgangDoc==1)||(checkDocNgang==1&&checkNgangDoc==2)){
					return 4;//u tron
				}
				else if(checkDocNgang==2||checkNgangDoc==2){
					return 4;//u tron
				}
				else if(checkDocNgang==1||checkNgangDoc==1){
					return 2;//u thuong
				}
				else return 0;//khong u
			}
		}
		
		return 0;
	}
}